import { LocalstorageDataManage } from './../../common/localstoreage/localstoreageDataManage';
import { AppStateService } from './../../app.chef.component';
import { BaseComponent } from './../../components/baseComponent';
import { Subject, Observable } from 'rxjs';
import { FrontStoreStocksService } from './../../services/frontStoreStocksService';
import { AddScrap } from './addScrap';
import { currentProject } from './../../../currentConfig';
import { NavigateManage } from './../../common/navigateManage';
import { PagingMessage, YScrollableContainer } from './../../components/scrollable/y-scrollable-container';
import { Component, AfterViewInit, OnInit, ViewChild } from '@angular/core';
import { MdDialog, MdDialogRef } from '@angular/material';
import { StorageViewModel } from "../../models/storageReceipt";
import { Debounce } from "../../common/decorator/index";
@Component({
    selector: 'chef-scrap',
    templateUrl: './chefScrap.component.html',
    styleUrls: ['./chefScrap.component.scss']
})

export class ChefScrapComponent extends BaseComponent implements OnInit, AfterViewInit {
    @ViewChild(YScrollableContainer)
    private yScrollableContainer: YScrollableContainer;

    constructor(
        public dialog: MdDialog,
        private frontStoreStockService: FrontStoreStocksService,
        private appStateService: AppStateService
        // private dialogRef: MdDialogRef<AddScrap>
    ) {
        super()
    }

    private scrapList$;
    private addScrap$;
    private terminalName;

    @Debounce(500)
    yScrollableContainerChildrenChanged() {
        this.yScrollableContainer && this.yScrollableContainer.childrenChanged();
    }

    ngOnInit() {
        this.addScrap$ = new Subject();
        const scraped$ = Observable.fromPromise(this.frontStoreStockService.getScrapOutStocks());
        this.scrapList$ = scraped$.merge<Array<StorageViewModel>>(this.addScrap$).scan((acc, value) => {
            return [...value, ...acc]
        }, []).map(scraps => scraps.map(scrap => {
            scrap.scrapCategoryQuantity = scrap.storageReceiptItems.length;
            scrap.scrapQuantity = scrap.storageReceiptItems.map(x => x.quantity).reduce((pre, cur) => { return pre + cur }, 0);
            return scrap;
        })).share();
        this.subscriptions.push(this.scrapList$.subscribe(res => console.log('scrap', res)));
        this.subscriptions.push(this.scrapList$.subscribe(() => {
            this.yScrollableContainerChildrenChanged();
        }))
        let terminal = LocalstorageDataManage.current.getTerminal();
        this.terminalName = terminal && terminal.name;

    }

    ngAfterViewInit() {

    }

    pagingMessageChange(event) {
        console.log("***", event)
        this.appStateService.changePagingInfo(event);
    }

    close() {
        NavigateManage.current.navigate(`${currentProject}Home`);
    }

    open() {
        const dialogRef = this.dialog.open(AddScrap, {
            height: '100%',
            width: '70%'
        });

        this.subscriptions.push(dialogRef.afterClosed().subscribe(res => {
            if (res) {
                this.addScrap$.next([res])
            }
        }))
    }

}